﻿//***************************************************************
// 脚本名称：EntityManager.cs
// 类创建人：周  波
// 创建日期：2017.02
// 功能描述：实体管理器
//***************************************************************
using System.Collections.Generic;
using UnityEngine;

public class EntityManager : Kernel<EntityManager>
{
    /// <summary>
    /// 实体显示对象根节点
    /// </summary>
    GameObject m_RootGameObject = null;

    /// <summary>
    /// 主角脚本
    /// </summary>
    Master m_Master = null;

    /// <summary>
    /// 除主角外的其他实体对象
    /// （key:实体实例id，value:实体脚本）
    /// </summary>
    Dictionary<ulong, Entity> m_EntityDict = new Dictionary<ulong, Entity>();

    /// <summary>
    /// 创建实体
    /// </summary>
    /// <param name="entityInitParam">实体初始化参数</param>
    /// <returns>true：创建成功；false：创建失败</returns>
    public bool CreateEntity(EntityInitializeParam entityInitParam)
    {
        if (entityInitParam == null)
        {
            Debug.LogError("The entity param is null!");
            return false;
        }
        if (m_RootGameObject == null)
        {
            m_RootGameObject = new GameObject("[Entities]");
            DontDestroyOnLoad(m_RootGameObject);
        }

        GameObject obj = new GameObject(entityInitParam.EntityName);
        obj.transform.parent = m_RootGameObject.transform;

        if (entityInitParam.EntityType == EntityType.Master)
        {
            if (m_Master != null)
            {
                GameObject.Destroy(obj);
                Debug.LogError("The master cann't be created repeatly!");
                return false;
            }
            Master master = obj.AddComponent<Master>();
            if (!master.Initialize(entityInitParam))
            {
                GameObject.Destroy(obj);
                return false;
            }
            m_Master = master;
            return true;
        }
        else
        {
            if (m_EntityDict.ContainsKey(entityInitParam.EntityUUID))
            {
                GameObject.Destroy(obj);
                Debug.LogError(string.Format("The entity[id = {0}, type = {1}, name = {2}] cann't be created repeatly!", entityInitParam.EntityUUID, entityInitParam.EntityType, entityInitParam.EntityName));
                return false;
            }

            Entity script = null;
            switch (entityInitParam.EntityType)
            {
                case EntityType.Monster:
                    break;
                default:
                    GameObject.Destroy(obj);
                    return false;
            }

            if (!script.Initialize(entityInitParam))
            {
                GameObject.Destroy(obj);
                return false;
            }

            m_EntityDict.Add(entityInitParam.EntityUUID, script);
            return true;
        }
    }

    /// <summary>
    /// 删除实体
    /// </summary>
    /// <param name="entityID">实体实例id</param>
    public void DestroyEntity(ulong entityID)
    {
        if (m_Master != null && entityID == m_Master.EntityUUID)
        {
            m_Master.Uninitialize();
            GameObject.Destroy(m_Master.MyGameObject);
            m_Master = null;
            return;
        }

        Entity entity = null;
        m_EntityDict.TryGetValue(entityID, out entity);
        if (entity != null)
        {
            entity.Uninitialize();
            GameObject.Destroy(entity.MyGameObject);
            m_EntityDict.Remove(entityID);
        }
    }

    /// <summary>
    /// 删除所有实体
    /// </summary>
    /// <param name="isContainsMaster">是否包含主角</param>
    public void DestroyAllEntitys(bool isContainsMaster = false)
    {
        List<ulong> deletedEntityList = new List<ulong>();
        foreach (var item in m_EntityDict)
        {
            deletedEntityList.Add(item.Key);
        }

        for (int i = 0; i < deletedEntityList.Count; i++)
        {
            Entity entity = m_EntityDict[deletedEntityList[i]];
            if (entity != null)
            {
                entity.Uninitialize();
                GameObject.Destroy(entity.MyGameObject);
                m_EntityDict.Remove(deletedEntityList[i]);
            }
        }
        deletedEntityList.Clear();

        if (isContainsMaster)
        {
            if (m_Master != null)
            {
                m_Master.Uninitialize();
                GameObject.Destroy(m_Master.MyGameObject);
                m_Master = null;
            }
        }
    }

    /// <summary>
    /// 获取到实体
    /// </summary>
    /// <param name="entityID">实体实例id</param>
    /// <returns>实体脚本</returns>
    public Entity GetEntity(ulong entityID)
    {
        if (m_Master != null && entityID == m_Master.EntityUUID)
        {
            return m_Master;
        }

        Entity entity = null;
        m_EntityDict.TryGetValue(entityID, out entity);
        return entity;
    }

    /// <summary>
    /// 获取实体
    /// </summary>
    /// <typeparam name="T">实体类型</typeparam>
    /// <param name="entityID">实体ID</param>
    /// <returns>如果未找到 返回null</returns>
    public T GetEntity<T>(ulong entityID) where T : Entity
    {
        Entity entity = GetEntity(entityID);
        return (entity as T);
    }

    /// <summary>
    /// 获取主角实体对象
    /// </summary
    /// <returns>主角对象实体脚本</returns>
    public Master GetMaster()
    {
        return m_Master;
    }
}
